<?php
/** 商品 */
namespace app\api\model;

use app\api\model\BaseModel;

/** */

class Product extends BaseModel{
    //隐藏字段
    protected $hidden =[
        'delete_time','main_img_id','pivot','from','category_id',
        'create_time','update_time'];
    
    public function getMainImgUrlAttr($value,$data){
        return $this->prefixImgUrl($value,$data);
    }

    //商品详情
    public function imgs(){
        return $this->hasMany('ProductImage','product_id','id');
    }

    public function properties(){
        return $this->hasMany('ProductProperty','product_id','id');
    }

    //商品数
    public static function getMostRecent($count){
        $products=self::limit($count)
            ->order(['create_time desc'])//('create_time'=>'desc')
            ->select();
        return $products;
    }

    public static function getPriductsByCategoryID($categoryID){
        $products=self::where('category_id','=',$categoryID)
            ->select();
        return $products;
    }

    //商品详情页
    public static function getProductDetail($id){
        $product=self::with([
            'imgs'=>function($query){
                $query->with(['imgUrl'])
                ->order('order','asc');
                }
            ])
            ->with(['properties'])
            ->find($id);
        return $product;
    }
}


